package k2_oop.p9_collection.demo12;

import java.util.*;

public class MainApp {

    //先声明一个盛放52张牌的扑克列表
    private static ArrayList<Poker> pokerList = new ArrayList<>();

    //初始化扑克牌的所有数值
    public static String[] nums = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
    //初始化扑克牌的所有花色
    public static String[] cardType = {"方块","草花","红桃","黑桃"};

    static {

        for (int i = 0; i < cardType.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                pokerList.add(new Poker(cardType[i],nums[j],i,j));
            }
        }

        System.out.println(pokerList);
    }

    /**
     * 将集合中的所有扑克重新洗牌,并将扑克输出(打印出来即可)
     */
    public static void shufflePoker(){
        Collections.shuffle(pokerList);
        System.out.println(pokerList);
    }

    /**
     * 对乱序的扑克牌重新排序
     */
    public static void sortPoker(){
        Collections.sort(pokerList);
        System.out.println(pokerList);
    }

    /**
     * 随机获取两张扑克牌
     * @return
     */
    public static Poker[] getTwoPoker(){

        Random random = new Random();

        int firstIndex = random.nextInt(pokerList.size());
        int secondIndex = 0;

        do{
            secondIndex = random.nextInt(pokerList.size());
        }while(firstIndex==secondIndex);

        return new Poker[]{pokerList.get(firstIndex),pokerList.get(secondIndex)};
    }

    /**
     * 比较两张牌的大小
     * @param poker1
     * @param poker2
     * @return
     */
    public static void compareTwoCard(Poker poker1,Poker poker2){

        List<Poker> twoPokers = Arrays.asList(poker1,poker2);

        twoPokers.stream()
                .sorted((p1,p2)->{
                    int p1TypeIndex = p1.getTypeIndex();
                    int p2TypeIndex = p2.getTypeIndex();
                    int p1NumIndex = p1.getValueIndex();
                    int p2NumIndex = p2.getValueIndex();

                    if(p1NumIndex==p2NumIndex){
                        return p1TypeIndex-p2TypeIndex;
                    }else{
                        return p1NumIndex-p2NumIndex;
                    }
                })
                .forEach(System.out::println);
    }

    public static void main(String[] args) {
//        MainApp.shufflePoker();
//        MainApp.sortPoker();
        Poker[] pokers = MainApp.getTwoPoker();
        System.out.println(Arrays.toString(pokers));
        MainApp.compareTwoCard(pokers[0],pokers[1]);
    }

}
